﻿namespace Acme.Marketing.DataAccess
{
    using System.Linq;
    using Data;

    public class AdvertisementRepository : IMarketingRepository<Advertisement>
    {
        private readonly MarketingContext _marketingContext;

        private bool _disposed;

        public AdvertisementRepository(MarketingContext context)
        {
            _marketingContext = context;
        }

        public Advertisement Get(int id)
        {
            return _marketingContext.Advertisements.FirstOrDefault(ad => ad.AdvertisementId.Equals((id)));
        }

        public void Attach(Advertisement entity)
        {
            _marketingContext.Advertisements.Attach(entity);
        }

        public IQueryable<Advertisement> GetAll()
        {
            return _marketingContext.Advertisements;
        }

        public void Insert(Advertisement entity)
        {
            _marketingContext.Advertisements.Add(entity);
            _marketingContext.SaveChanges();
        }

        public void Delete(Advertisement entity)
        {
            _marketingContext.Advertisements.Remove(entity);
            _marketingContext.SaveChanges();
        }

        public void SaveChanges()
        {
            _marketingContext.SaveChanges();
        }

        public void Dispose()
        {
            Dispose(true);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                if (disposing)
                {
                    _marketingContext.Dispose();
                }
            }
            _disposed = true;
        }
    }
}